package com.fuda.trade.ui.strategies

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fuda.trade.MyApplication
import com.fuda.trade.ZenohManager
import com.fuda.trade.model.FudaThing
import com.fuda.trade.model.FudaThingId
import com.fuda.trade.model.TradeStrategy
import com.fuda.trade.model.TradeStrategyData
import io.zenoh.sample.Sample
import kotlinx.coroutines.launch

class StrategiesViewModel : ViewModel() {

    companion object {
        val TAG: String = StrategiesViewModel::class.java.simpleName
    }

    private val _strategies = MutableLiveData<List<TradeStrategy>>()
    val strategies: LiveData<List<TradeStrategy>>
        get() = _strategies

    init {
        refreshData()
    }

    private fun updateStrategies(strategyList: List<TradeStrategy>) {
        _strategies.postValue(strategyList)
    }

    fun refreshData() {
        viewModelScope.launch {
            // Fetch the latest data from the server
            fetchLatestStrategiesFromServer { newCommands ->
                updateStrategies(newCommands)
            }
        }
    }

    private fun fetchLatestStrategiesFromServer(onSuccess: (List<TradeStrategy>) -> Unit) {

        ZenohManager.strategiesSelector?.let {
            MyApplication.zenohManager.select(it,
                object:ZenohManager.ZenohCallback{
                    override fun onSuccess(sample: Sample) {
                        val payload = sample.value.toString()
                        Log.d(TAG, "viewModel got:  $payload" )
                        val strategies = TradeStrategy.deserializeList(payload)
                        onSuccess(strategies)
                    }

                    override fun onError(error: String) {
                        Log.e(TAG, error)

                        // Simulate fetching data from the server
                        // Replace this with your actual network call
                        val newTradeStrategyData = listOf(
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy001")), data= TradeStrategyData.Short( volume = 500, desc="New command added")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy002")), data= TradeStrategyData.Long(volume = 200,desc="desc buy manual2")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy003")), data= TradeStrategyData.Long(volume = 300,desc="desc sell auto3")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy004")), data= TradeStrategyData.Long(volume = 400,desc="desc sell manual4")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy005")), data= TradeStrategyData.Short( volume = 100,desc="desc buy auto1")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy006")), data= TradeStrategyData.Short( volume = 200,desc="desc buy auto2")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy007")), data= TradeStrategyData.Short( volume = 300,desc="desc buy auto3")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy008")), data= TradeStrategyData.Short( volume = 400,desc="desc buy auto4")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy009")), data= TradeStrategyData.Short( volume = 100,desc="desc buy auto1")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy010")), data= TradeStrategyData.Short( volume = 200,desc="desc buy auto2")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy011")), data= TradeStrategyData.Short( volume = 300,desc="desc buy auto3")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy012")), data= TradeStrategyData.Short( volume = 400,desc="desc buy auto4")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy013")), data= TradeStrategyData.Short( volume = 100,desc="desc buy auto1")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy014")), data= TradeStrategyData.Short( volume = 200,desc="desc buy auto2")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy015")), data= TradeStrategyData.Short( volume = 300,desc="desc buy auto3")),
                            TradeStrategy( id= FudaThing("trade_strategy", FudaThingId("strategy016")), data= TradeStrategyData.Short( volume = 400,desc="desc buy auto4")),
                        )
                         onSuccess(newTradeStrategyData)
                    }
                }
            )
        }
    }

}